<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { userRegisterService, userLoginService } from '@/api/user'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
const router = useRouter()
const userStore = useUserStore()
const isRegister = ref(false)
const triggerRegister = () => {
  isRegister.value = !isRegister.value
  reset()
}
const form = ref()
// 整个用于提交的form数据对象
const forModel = ref({
  username: '',
  password: '',
  repassword: ''
})
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('密码不能为空'))
  } else if (value !== forModel.value.password) {
    callback(new Error('第二次输入密码和前面不相等'))
  } else {
    callback()
  }
}
// 整个表达的校验规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 15, message: 'Length should be 5 to 15', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [{ validator: validatePass2, trigger: 'blur' }]
}
// 注册
const register = async () => {
  await form.value.validate()
  await userRegisterService(forModel.value)
  ElMessage.success('注册成功')
  isRegister.value = false
  reset()
}
// 重置表单
const reset = () => {
  forModel.value.password = ''
  forModel.value.repassword = ''
  forModel.value.username = ''
}
// 登录
const login = async () => {
  await form.value.validate()
  const { token } = await userLoginService(forModel.value)
  userStore.setToken(token)
  // 提示登录成功
  ElMessage.success('登录成功')
  router.push('/')
}
</script>

<template>
  <!-- 
    1.el-form =>:model='ruleform', 绑定整个form的数据对象
    2.el-form =>:rules='rules',绑定整个rules规则对象
    3.表单元素 => v-model='ruleform.属性' 给表单元素，绑定form子属性
    4.表单元素上的校验 => prop='xxx' ，这个是填写rules的属性。
   -->
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <el-form
        ref="form"
        size="large"
        :model="forModel"
        :rules="rules"
        autocomplete="off"
        v-if="isRegister"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            placeholder="请输入用户名"
            v-model="forModel.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            type="password"
            v-model="forModel.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            :prefix-icon="Lock"
            type="password"
            v-model="forModel.repassword"
            placeholder="请输入再次密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="button" @click="register" type="primary" auto-insert-space>
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="triggerRegister"> ← 返回 </el-link>
        </el-form-item>
      </el-form>
      <el-form ref="form" size="large" :model="forModel" :rules="rules" autocomplete="off" v-else>
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            v-model="forModel.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            :prefix-icon="Lock"
            type="password"
            v-model="forModel.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button class="button" @click="login" type="primary" auto-insert-space>登录</el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="triggerRegister"> 注册 → </el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;

  .bg {
    background:
      url('@/assets/logo2.png') no-repeat 60% center / 240px auto,
      url('@/assets/login_bg.jpg') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }

  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;

    .title {
      margin: 0 auto;
    }

    .button {
      width: 100%;
    }

    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
